package com.syezon.wifikey.wifi.dialog;

import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.net.wifi.WifiConfiguration;
import android.support.annotation.NonNull;
import android.view.Gravity;
import android.view.View;
import android.view.WindowManager;
import android.widget.TextView;

import com.syezon.android.base.utils.ToastUtils;
import com.syezon.wifikey.R;
import com.syezon.wifikey.utils.WifiUtil;
import com.syezon.wifikey.wifi.WifiConnectService;
import com.syezon.wifikey.wifi.info.WifiInfo;
import com.syezon.wifikey.wifi.WifiDetailActivity;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by Administrator on 2018/9/11.
 */

public class WifiInfoDialog extends Dialog {

    @BindView(R.id.tv_ssid)
    TextView mTvSsid;
    @BindView(R.id.tv_connect)
    TextView mTvConnect;
    @BindView(R.id.tv_detail)
    TextView mTvDetail;
    @BindView(R.id.tv_disconnect)
    TextView mTvDisconnect;

    private WifiInfo mWifiInfo;

    public WifiInfoDialog(@NonNull Context context) {
        this(context, 0);
    }

    public WifiInfoDialog(@NonNull Context context, int themeResId) {
        super(context, themeResId);
        setContentView(View.inflate(getContext(), R.layout.dialog_wifi_info, null));
        ButterKnife.bind(this);
    }

    public void setWifiBean(WifiInfo wifiInfo) {
        this.mWifiInfo = wifiInfo;
        mTvSsid.setText(wifiInfo.getSsid());
        if (wifiInfo.isConnected()) {
            mTvDisconnect.setVisibility(View.VISIBLE);
        } else {
            mTvDisconnect.setVisibility(View.GONE);
        }
    }

    @OnClick({R.id.tv_connect, R.id.tv_detail, R.id.tv_disconnect, R.id.tv_cancel})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tv_connect:
                connectWifi();
                dismiss();
                break;
            case R.id.tv_detail:
                Intent detailIntent = new Intent();
                detailIntent.setClass(getContext(), WifiDetailActivity.class);
                detailIntent.putExtra(WifiDetailActivity.ARG_WIFI_INFO, mWifiInfo);
                getContext().startActivity(detailIntent);
                dismiss();
                break;
            case R.id.tv_disconnect:
                disconnectWifi();
                dismiss();
                break;
            case R.id.tv_cancel:
                dismiss();
                break;
        }
    }

    private void connectWifi() {
        if (mWifiInfo.isConnected()) {
            ToastUtils.showShort("当前WiFi已连接!");
        } else {
            if (!mWifiInfo.isAvailable()) {
                ToastUtils.showShort("当前WiFi不可用!");
            } else {
                if (mWifiInfo.isConfig() && mWifiInfo.getWifiConfiguration() != null && mWifiInfo.getWifiConfiguration().networkId > 0) {
                    WifiConnectService.getInstance().connect(mWifiInfo);
                } else {
                    EnterPwdDialog.enterPwd(getContext(), mWifiInfo, false);
                }
            }
        }
    }

    private void disconnectWifi() {
        try {
            WifiUtil.disconnect();
//            WifiConfiguration wifiConfiguration = mWifiInfo.getWifiConfiguration();
//            if (wifiConfiguration != null && wifiConfiguration.networkId > 0) {
//                WifiUtil.disableNetwork(wifiConfiguration.networkId);
//            }
        } catch (Exception e) {
        }
    }

    @Override
    public void show() {
        getWindow().setGravity(Gravity.BOTTOM);
        getWindow().setLayout(WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.WRAP_CONTENT);
        super.show();
    }
}
